package com.iemes.controller.equip;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AbolishappFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EquipAccepFormMap;
import com.iemes.entity.EquipCategoryFormMap;
import com.iemes.entity.EquipFileFormMap;
import com.iemes.entity.ErpEquipmentapply;
import com.iemes.entity.ReceiveEquipFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.ReceiveEquipMapper;
import com.iemes.plugin.PageView;
import com.iemes.upload.UploadUtil;
import com.iemes.util.Common;

@Controller
@RequestMapping("equip")
public class ReceiveEquipController extends BaseController {
	@Inject
	private ReceiveEquipMapper receive;

	@RequestMapping(value = "equip_receive", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/receive/equipbom_list";
	}
	@ResponseBody
	@RequestMapping("equip_receiveByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		ReceiveEquipFormMap receiveEquipFormMap = getFormMap(ReceiveEquipFormMap.class);
		if (receiveEquipFormMap.get("state")==null) {
			receiveEquipFormMap.put("state", 1);
		}
		List<Map<String, Object>> equipReceive = receive
				.getRecivefuzzy(receiveEquipFormMap);	
		try {
			LuogicPaging(pageNow, pageSize, equipReceive);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}

	@RequestMapping("equip_receiveadds")
	public String addUI(Model model, HttpServletRequest request)
			throws Exception {		
		return Common.BACKGROUND_PATH + "/equip/receive/equipbom_add";
	}

	@ResponseBody
	@RequestMapping("equipreceive_addEntity")
	// @Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "工具管理", methods = "工具管理-添加工具领用")// 凡需要处理业务逻辑的.都需要记录操作日	
	public String addEntity() throws Exception {
		ReceiveEquipFormMap receiveEquipFormMap = getFormMap(ReceiveEquipFormMap.class);
		// 获取request  
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		receiveEquipFormMap.put("peopleInformationId", userFormMap.get("id"));
		receive.addEntity(receiveEquipFormMap);
		return "success";
	}
	
	@RequestMapping("receive_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");	
		ReceiveEquipFormMap equipfile = receive.findbyFrist("id", id,
				ReceiveEquipFormMap.class);
		model.addAttribute("equipfile", equipfile);
		return Common.BACKGROUND_PATH + "/equip/receive/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "receive_editEntity")
	@SystemLog(module = "工具管理", methods = "工具管理-修改工具领用")// 凡需要处理业务逻辑的.都需要记录操作日	
	public String editEntity() throws Exception {
		ReceiveEquipFormMap receiveEquipFormMap = getFormMap(ReceiveEquipFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean receive_deleteEntity
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		receiveEquipFormMap.put("peopleInformationId", userFormMap.get("id"));
		receive.editEntity(receiveEquipFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "receive_deleteEntity")
	@SystemLog(module = "工具管理", methods = "工具管理-删除工具领用")
	public String deleteEntity() throws Exception {
		ReceiveEquipFormMap receiveEquipFormMap = getFormMap(ReceiveEquipFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean receive_deleteEntity
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			receiveEquipFormMap.put("peopleInformationId", userFormMap.get("id"));
			receiveEquipFormMap.set("state", 1);
			receiveEquipFormMap.put("id", str[i]);
			receive.editEntity(receiveEquipFormMap);
		}
		return "success";
	}

}
